#pragma once
#include<iostream>
#include<semaphore.h>
const int number = 5;
namespace SemModule
{
    class Sem
    {
    public:
        Sem(int num = number)
        {
            int n = sem_init(&_sem,0,number);
            if(n != 0)
            {
                std::cout << "sem_init error" << std::endl;
            }
        }
        void P()
        {
            int n = sem_wait(&_sem);
            if(n != 0)
            {
                std::cout << "sem_wait error" << std::endl;
            }
        }
        void V()
        {
            int n = sem_post(&_sem);
            if(n != 0)
            {
                std::cout << "sem_post error" << std::endl;
            }
        }

        ~Sem()
        {
            int n = sem_destroy(&_sem);
            if(n != 0)
            {
                std::cout << "sem_destory error" << std::endl;
            }
        }

    private:
        sem_t _sem;
    };
}